﻿using CoreShop.Library;
using CoreShop.Library.Units;
using CoreShop.Model;
using CoreShop.Model.FromBody;
using CoreShop.Model.UI;
using CoreShop.Services;
using CoreShop.Services.Services.System;
using MySqlX.XDevAPI.Common;
using NAudio.CoreAudioApi;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Sunny.UI;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Security.Policy;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using static CoreShop.Services.PosRemoteServices;

namespace CoreShop.App.Units.Interface
{
    public class OrderApiUtil
    {
        /// <summary>
        /// 修改订单状态
        /// </summary>
        public static async Task<ReturnModel> UpdateOrderStatus(string orderSn, sysuser sysuser)
        {
            ReturnModel returnModel = new ReturnModel();
            returnModel.MessageOrString = "远程服务器无法访问。";
            try
            {
                JObject keyValues = new JObject();
                keyValues.Add(new JProperty("orderSn", orderSn));
                //ReturnModel returnModel = HttpHelper.PostSendMessage(baseUrl, $"{SERVER.Order}{API.BranchPriceUpdate}", keyValues.ToString(), 60, "", sysuser);
                returnModel = await HttpHelperAsync.PostDataAsync(ApiConsVars.baseUrl, $"{SERVER.Order}{API.UpdateOrderStatus}", keyValues.ToString(), 60, "", AppConstVars.sysUser);
                return returnModel;
            }
            catch (Exception ex) { string msg = ex.Message; }
            return returnModel;
        }

        public static bool SendOrderUp(sysuser sysUser, corecmsstore sysStore, string classNo, long orderId)
        {
            Thread newThread = new Thread(async () => await SendOrderThread(sysUser, sysStore, classNo, orderId));
            newThread.Start();
            return true;
        }

        public static async Task<AsyncReturnModel> SendOrderThread(sysuser sysUser, corecmsstore sysStore, string classNo, long orderId, bool isSaveLog = false)
        {
            AsyncReturnModel result = new AsyncReturnModel();
            var posOrderHeadDto = new OrderBusiness().GetUpLoadOrderData(sysUser, sysStore, classNo, orderId);
            result = await SendOrderAsync(posOrderHeadDto, isSaveLog);
            if ((result.code == 200) || ((result.code == 30001) && (result.msg.IndexOf("重复") > 0)))
            {
                new corecmsfoodordermasterServices().UpdateUpLoadById(orderId);
                result.msg = "上传成功。";
                result.status = true;
                return result;
            }
            return result;
        }


        /// <summary>
        /// 上传订单
        /// </summary>
        /// <param name="posData"></param>
        /// <returns></returns>
        public static async Task<AsyncReturnModel> SendOrderAsync(PosOrderHeadDto posData, bool isSaveLog = false)
        {
            AsyncReturnModel res = new AsyncReturnModel();
            string postStr = "";
            string resultStr = "";
            try
            {
                postStr = JsonConvert.SerializeObject(posData);
                var url = $"{SERVER.Order}{API.InsertPosOrder}";
                ReturnModel returnModel = await HttpHelperAsync.PostDataAsync(ApiConsVars.baseUrl, url, JsonConvert.SerializeObject(posData), 60, "", AppConstVars.sysUser);
                resultStr = returnModel.MessageOrString;
                res = JsonConvert.DeserializeObject<AsyncReturnModel>(resultStr);
                return res;
            }
            catch (Exception ex)
            {
                res.msg = ex.Message;
                res.code = 1;
                if (isSaveLog)
                {
                   await new corecmsprinttasklogServices().InsertAsync("上传数据提交:" + postStr);
                   await new corecmsprinttasklogServices().InsertAsync("上传数据返回:" + resultStr);
                }
            }
            return res;
        }
    }
}
